//推送全体管理消息、加uid推送个人
exports.allOrganizationTemplateMessage = function (req, res) {
    //获取bodyParams参数
    var bodyParams = req.routeInfo.bodyParams;

    //最终结果
    var resultData = {};

    var openidSum = 0;

    var successSum = 0;

    //任务流创建
    async.waterfall([

        //指定发送私信
        function (cb) {
            if (bodyParams.uid) {
                console.log('指定发送私信');

                R.SEND_HTTP(
                    req, {
                        url: CORE_APIConfig.coreAPI.searchOrganizationUser + '/ou_uid/' + bodyParams.uid,
                        method: 'POST'
                    },
                    function (err, data) {
                        if (data.data == '') {
                            return RES.response(res, false, "UID不存在");
                        }

                        oneOpen = data.data;

                        if (!oneOpen[0].openid) {

                            return RES.response(res, false, "该管理员没有OPENID无法接收推送");

                        } else {

                            var tempOpenids = oneOpen[0].openid.replace(/\*/g, "-"); //把'is'替换为空字符串

                            R.SEND_HTTP(
                                req, {
                                    url: "http://127.0.0.1:3400/weixin/templateMessagesSent",
                                    method: 'POST',
                                    data: {
                                        openid: tempOpenids,
                                        type: "baoMingChengGongTongZhi",
                                        goUrls: 'http://manage.hxzbi.com/#/marketing_detail', //跳转地址
                                    }
                                },
                                function (err, data) {

                                    return RES.response(res, true, data);

                                }
                            );

                        }

                    }
                );

            } else {
                cb();
            }
        },
        //查询机构管理员的openid
        function (cb) {
            console.log('查询机构管理员的openid')
            R.SEND_HTTP(
                req, {
                    url: CORE_APIConfig.coreAPI.searchOrganizationUser,
                    method: 'POST'
                },
                function (err, data) {
                    console.log(data);
                    open = data.data;
                    openidSum = data.data.length;
                    cb();
                }
            );

        },
        //循环发送私信
        function (cb) {
            console.log('循环发送私信');
            var tasks = [];
            open.forEach(function (item, i) {
                if (item.openid != null && item.openid.length > 5) {
                    console.log(item.openid)
                    tasks.push(function (cb) {

                        R.SEND_HTTP(
                            req, {
                                url: "http://127.0.0.1:3400/weixin/templateMessagesSent",
                                method: 'POST',
                                data: {
                                    openid: item.openid,
                                    type: "baoMingChengGongTongZhi",
                                    goUrls: 'http://manage.hxzbi.com/#/marketing_detail', //跳转地址
                                }
                            },
                            function (err, data) {

                                successSum++;

                                cb();

                            }
                        );

                    });
                }
            });
            async.waterfall(tasks, cb);
        },
        function (cb) {
            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.addMessagePushRecord,
                    method: 'POST',
                    data: {
                        mpr_message: bodyParams.mpr_message,
                        mpr_sum: openidSum,
                        mpr_arrive: successSum,
                        mpr_fail: openidSum - successSum,
                        mpr_title: bodyParams.mpr_title,
                        mpr_user: bodyParams.mpr_user
                    }
                },
                function (err, data) {

                    //判断是否有错误信息
                    if (err) {
                        return cb(err, data);
                    }

                    console.log(data);
                    cb();
                }
            );
        }
    ], function (err, result) {

        //返回最终拼接结果
//				console.log('最终结果。。。。')
//				console.log(resultData)

        RES.response(res, true, resultData);

    });

}